﻿using Kinlo.Entitys;
using Kinlo.MESDocking;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Commands;
using UIWindows.Domains;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Models.ParameterModels;
using UIWindows.Tools.Helper;
using XCode;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels.Dialogs
{
    public class ProductCodeDialogViewModel : UIWindows.Domains.ViewModelBase
    {
        IMESInteraction _getByProductCodeInterface;
        ParameterStatic _parameterStatic;
        public SnackbarMessageQueue MyMessageQueue { get; set; } = new SnackbarMessageQueue(new TimeSpan(0, 0, 3));
        public string ProductBarcode { get; set; } = string.Empty;
        CollectionItemsParameterModel _collectionItems;
        public ProductCodeDialogViewModel(IMESInteraction getByProductCodeInterface, ParameterStatic parameterStatic, CollectionItemsParameterModel collectionItems)
        {
            _getByProductCodeInterface = getByProductCodeInterface;
            _parameterStatic = parameterStatic;
            _collectionItems = collectionItems;
        }

        public DelegateCommand GetProductCodeCMD => new DelegateCommand(() =>
        {
            if (string.IsNullOrEmpty(ProductBarcode))
            {
                MyMessageQueue.Enqueue("条码不能为空！", "知道啦", () => { });
                return;
            }
            try
            { //工单获取
                Dictionary<string, object> param = new Dictionary<string, object>
                {
                    { "machineNo", _parameterStatic.CollectionItemsParameter.EquipNum },
                    { "producttype", _parameterStatic.CollectionItemsParameter.ProductType.ToString() },

                    { "parameter", new Dictionary<string, object>()
                        {
                            { "tenantID", _parameterStatic.CollectionItemsParameter.ProductionLineNo },//产线编号
                            { "technicsProcessCode", _parameterStatic.CollectionItemsParameter.TechnicsProcessCode },//工序编码 
                            { "productCode", ProductBarcode },//电芯条码                                                                 
                        }
                    },
                };
                var mesResult = _getByProductCodeInterface.Interaction(_parameterStatic.MESParameter.BaseUrl + _parameterStatic.MESParameter.GetByProductCodeRoute,
                      _parameterStatic.MESParameter.GetByProductCodeTime,
                      _parameterStatic.MESParameter.GetByProductCodeNumber,
                      DateTime.Now, null, param);
                if (mesResult == ResultTypes.合格)
                {
                    _parameterStatic.CollectionItemsParameter.WorkOrdersCollection.Clear();
                    //解析返回的代码
                    foreach (var item in param["result"] as List<Dictionary<string, object>>)
                    {
                        _parameterStatic.CollectionItemsParameter.WorkOrdersCollection.Add(item.TryGetValue("produceOrderCode", out var v) ? v.ToString() : string.Empty);
                    }
                    if (_parameterStatic.CollectionItemsParameter.WorkOrdersCollection.Count > 0)
                    {
                        _collectionItems.WorkOrder = _parameterStatic.CollectionItemsParameter.WorkOrder = _parameterStatic.CollectionItemsParameter.WorkOrdersCollection[0];
                    }
                    _ = $"[工单获取] 成功！".MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
                    if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); } 
                }

            }
            catch (Exception ex)
            {

                _ = $"[工单获取] 发生异常：{ex}".MessageDialogs(MessageLevelType.警告, true, PopUpTypes.Global);
            }


        });

        public Commands.DelegateCommand CloseCMD => new Commands.DelegateCommand(() =>
        {
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString(), false); }

        });
    }
}
